//
//  SearchController.m
//  LoveFreshBeen
//
//  Created by 魏根 on 2016/10/17.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "SearchController.h"
#import "HotQueryModel.h"
#import "NSString+Extension.h"
#import "HMHomeFirstModel.h"
#import "HMHomeCollectionViewCell.h"
#import "ShoppingCartViewController.h"
#import "AppDelegate.h"
#import <JSBadgeView.h>
#import "ShopCartGoodsListTool.h"


static NSString* reuserIdC = @"cellcollection";

@interface SearchController ()<UICollectionViewDelegate,UICollectionViewDataSource,CAAnimationDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)UIButton *searchBtn;

@property(nonatomic,strong)NSArray *queryArray;

@property(nonatomic,weak)UIButton *lastBtn;

@property(nonatomic,assign)float BtnMaxX;

@property(nonatomic,weak)UILabel *buy_label;

@property(nonatomic,strong)NSArray *searchResultArray;

@property(nonatomic,weak)UICollectionView *searchCollectionView;

@property(nonatomic,strong)CALayer* myLayer;

@property(nonatomic,weak)UIScrollView *myScrollView;

@property(nonatomic,weak)UIView *topView;

@property(nonatomic,assign)CGFloat buttomY;

@property(nonatomic,strong)UIWindow *myShoppingCart;

@property(nonatomic,strong)UIButton *shoppingCartBtn;

@property(nonatomic,weak)UIImageView *searchIcon;

@property(nonatomic,weak)UILabel *placeWord;

@property(nonatomic,weak)UITextField *searchText;

@property(nonatomic,strong)JSBadgeView *badgeView;

@property(nonatomic,strong)UIWindow *badgeWindow;

//@property(nonatomic,assign)NSInteger goodsCount;

@end

@implementation SearchController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加 scrollView
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    self.myScrollView = scrollView;
    
    [self.view addSubview:scrollView];
    
    scrollView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    [self getData];
    
//    [self addLabel];
    
    [self getSearchData];
}

- (void)getSearchData{
    
    [HMHomeFirstModel downLoadTheMessageFirst:@"8" andFinish:^(NSArray *array) {
        
        self.searchResultArray = array;
        
        [self.searchCollectionView reloadData];

    } andError:^{
        
        NSLog(@"加载搜索数据失败");
    }];
    
}

- (NSArray *)getData{
    
    if(!self.queryArray){
        
        HotQueryModel *queryModel = [[HotQueryModel alloc]init];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //下载数据
            [[NetWorkingTool shareInstance]getDatawithParam:@"6" WithUrlString:[NetWorkingTool shareInstance].params[@"6"] withSuccessBlock:^(NSDictionary *data) {
                
                [queryModel yy_modelSetWithDictionary:data[@"data"]];
                
                NSMutableArray *nmArray = [NSMutableArray array];
                
                for (NSString *str in queryModel.hotquery) {
                    
                    [nmArray addObject:str];
                }
                self.queryArray = nmArray;
                
            } withFailedBlock:^(NSError *error) {
                
                NSLog(@"%@",error);
                
            } withErrorBlock:^(NSString *message) {
                
                NSLog(@"%@",message);
                
            }];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //更新数据
                
            });
        });
    }
    
    return self.queryArray;
}

- (void)setQueryArray:(NSArray *)queryArray{
    
    _queryArray = queryArray;
    
    [self addLabel];
    
    [self addBtn];
}

- (void)viewDidLayoutSubviews{
    
    self.searchCollectionView.frame = CGRectMake(0, 90, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if (self.topView != nil) {
        
        [self addShoppingCart];
    }
}


//热门搜索
- (void)addLabel{
    
    _titleLabel = [[UILabel alloc]init];
    
    [self.myScrollView addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.myScrollView).offset(10);
        
        make.leading.equalTo(self.myScrollView).offset(10);
        
    }];
    
    _titleLabel.text = @"热门搜索";
    
    _titleLabel.textColor = [UIColor grayColor];
    
}
//热词
- (void)addBtn{
    
    for(int i = 0;i < self.queryArray.count;i++){
    
        UIButton *btn = [[UIButton alloc]init];
        
        [self.myScrollView addSubview:btn];
        
        btn.backgroundColor = [UIColor whiteColor];
        
        [btn setTitle:self.queryArray[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        btn.layer.cornerRadius = 15;
        
        btn.clipsToBounds = YES;
        
        CGSize titleSize = [self.queryArray[i] sizeWithFont:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(60, 30)];
        
        self.BtnMaxX += (10 + 30 + titleSize.width);
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (self.lastBtn == nil || (self.BtnMaxX > SCREEN_WIDTH && self.BtnMaxX - titleSize.width - 10 - 30 < SCREEN_WIDTH) || self.BtnMaxX > 2 * SCREEN_WIDTH){
                
                make.leading.equalTo(self.myScrollView).offset(10);
            }else{
                
                make.leading.equalTo(self.lastBtn.mas_trailing).offset(10);
            }
            
            if(self.BtnMaxX > 2 * SCREEN_WIDTH){
                
                make.top.equalTo(self.titleLabel.mas_bottom).offset(90);
                
            }else if(self.BtnMaxX > SCREEN_WIDTH){
                
                make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
            }else{
                
                make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
            }
            make.width.equalTo(@(titleSize.width + 30));
            
            make.height.equalTo(@(titleSize.height + 10));
        }];
        
        [btn addTarget:self action:@selector(hotSearchDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
        self.lastBtn = btn;
    }
    
    self.myScrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT);
    
    self.myScrollView.showsVerticalScrollIndicator = NO;
    
    self.myScrollView.showsHorizontalScrollIndicator = NO;
}

- (void)hotSearchDidClick:(UIButton *)btn{
    
    self.searchText.text = btn.titleLabel.text;
    
    [self performSelector:@selector(hiddenLabel:) withObject:_searchText];
    
    self.myScrollView.contentSize = CGSizeMake(0,_searchResultArray.count / 2* ((SCREEN_WIDTH - 20)/2)+ 90);
    
    for (UIView *subView in self.myScrollView.subviews) {
        
        [subView removeFromSuperview];
    }
    
    //上面⚠️view
    UIImage *icon_exclamationmark = [UIImage imageNamed:@"icon_exclamationmark"];
    
    UIImageView *icon_mark = [[UIImageView alloc]initWithImage:icon_exclamationmark];
    
    [icon_mark sizeToFit];
    
    UILabel *top = [[UILabel alloc]init];
    
    top.text = [NSString stringWithFormat:@"暂时没搜到 \"%@\" 相关产品",btn.titleLabel.text];
    
    top.font = [UIFont systemFontOfSize:14];
    
    top.textColor = [UIColor colorWithRed:148/255.0 green:107/255.0 blue:81/255.0 alpha:1.0];
    
    UILabel *base = [[UILabel alloc]init];
    
    base.text = @"换成其他搜索词试试";
    
    base.font = [UIFont systemFontOfSize:12];
    
    base.textColor = [UIColor colorWithRed:248/255.0 green:185/255.0 blue:50/255.0 alpha:1.0];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    
    [self.myScrollView addSubview:topView];
    
    topView.backgroundColor = [UIColor colorWithRed:249/255.0 green:242/255.0 blue:216/255.0 alpha:1.0];
    
    [topView addSubview:icon_mark];
    
    self.topView = topView;
    
    [icon_mark mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(topView).offset(20);
        
        make.centerY.equalTo(topView);
    }];
    
    [topView addSubview:top];
    
    [topView addSubview:base];
    
    [top mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(icon_mark.mas_trailing).offset(10);
        
        make.top.equalTo(topView).offset(10);
    }];
    
    [base mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(icon_mark.mas_trailing).offset(10);
        
        make.bottom.equalTo(topView).offset(-10);
    }];
    //大家都在买
    UILabel *label_buy = [[UILabel alloc]init];
    
    self.buy_label = label_buy;
    
    label_buy.text = @"大家都在买";
    
    label_buy.textColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1.0];
    
    label_buy.font = [UIFont systemFontOfSize:11];
    
    [self.myScrollView addSubview:label_buy];
    
    [label_buy mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(topView).offset(30);
        
        make.top.equalTo(topView.mas_bottom).offset(10);
    }];
    
    [self addConllectionView];
}
//添加collectionView
- (void)addConllectionView{
    
//    [self addShoppingCart];
    
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
    
    CGFloat item = (SCREEN_WIDTH - 20)/2;
    
    layout.itemSize = CGSizeMake(item - 10, item - 10);
    
    CGFloat collectionViewH = self.searchResultArray.count / 2 * (item - 10) + 50;
    
    layout.minimumLineSpacing = 5;
    
    layout.minimumInteritemSpacing = 5;
    
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    UICollectionView* collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    
    //设置collectionView不能滚动
    
    collection.scrollEnabled = NO;
    
    self.searchCollectionView = collection;
    
    [self.myScrollView addSubview:collection];
    //最底下那个"无更多商品"
    UILabel *buttomLabel = [[UILabel alloc]init];
    
    buttomLabel.text = @"无更多商品";
    
    buttomLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.myScrollView addSubview:buttomLabel];
    
    [buttomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.equalTo(@40);
        
        make.leading.trailing.equalTo(self.topView);
        
        make.top.equalTo(self.searchCollectionView.mas_bottom);
    }];
    
    [collection mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.buy_label.mas_bottom).offset(20);
        
        make.leading.trailing.equalTo(self.topView);
        
        make.width.equalTo(@(item - 10));

        make.height.equalTo(@(collectionViewH + 10));
    }];
    
    
    
    collection.bounces = NO;
    
    collection.delegate = self;
    
    collection.dataSource = self;
    
    collection.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    
}

//增加购物车
- (void)addShoppingCart{
    
    self.shoppingCartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
   
    [self.shoppingCartBtn setBackgroundColor:[UIColor colorWithRed:252/255.0 green:213/255.0 blue:51/255.0 alpha:1.0]];
    
    [self.shoppingCartBtn setImage:[UIImage imageNamed:@"v2_whiteShopSmall"] forState:UIControlStateNormal];
    
    self.shoppingCartBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.shoppingCartBtn.frame = CGRectMake(0, 0, 60, 60);
    
    [self.shoppingCartBtn addTarget:self action:@selector(jumpToShoppingCart) forControlEvents:UIControlEventTouchUpInside];
    
    self.myShoppingCart = [[UIWindow alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 70, SCREEN_HEIGHT - 70, 60, 60)];
    
    self.badgeWindow = [[UIWindow alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30, SCREEN_HEIGHT - 70, 22, 22)];
    
    self.badgeWindow.windowLevel = UIWindowLevelAlert+1;
    
    self.badgeWindow.backgroundColor = [UIColor clearColor];
    
   
    
    self.myShoppingCart.windowLevel = UIWindowLevelAlert+1;
    
    self.myShoppingCart.backgroundColor = [UIColor redColor];
    
    self.myShoppingCart.layer.cornerRadius = 30;
    
    self.myShoppingCart.layer.masksToBounds = YES;
    
    [self.myShoppingCart addSubview:self.shoppingCartBtn];
    
    [self.myShoppingCart makeKeyAndVisible];//关键语句,显示window
    
    [self.badgeWindow makeKeyAndVisible];
    
    //增加角标
    self.badgeView = [[JSBadgeView alloc]initWithParentView:self.badgeWindow alignment:JSBadgeViewAlignmentCenter];
    
    self.badgeView.backgroundColor = [UIColor redColor];
    
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(badgeNumberDidChange) name:@"changeGoodsNoti" object:nil];
    
    
        self.badgeView.badgeText = nil;
        
        self.badgeView.hidden = YES;
    
    [self badgeNumberDidChange];
   
}

- (void)badgeNumberDidChange{
    
    self.badgeView.badgeText = [NSString stringWithFormat:@"%lu",[[ShopCartGoodsListTool sharedGoodsListTool] goodCount]];
    
    if([self.badgeView.badgeText isEqualToString:@"0"]){
        
        self.badgeView.badgeText = nil;
        
        self.badgeView.hidden = YES;
    }else{
        
        self.badgeView.hidden = NO;
    }
    
    
    [self.badgeView setNeedsLayout];
}


/**
 *  关闭悬浮的window
 */
- (void)resignWindow
{
    
    
    [self.myShoppingCart resignKeyWindow];
    
    self.myShoppingCart = nil;
    
    [self.badgeWindow resignKeyWindow];
    
    self.badgeWindow = nil;
}


#pragma mark---------------collectionView的代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.searchResultArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //由于刷新多次,所以必须把xib的注册写入该方法中,否者会提示没有注册Xib
    UINib* nib = [UINib nibWithNibName:@"HMHomeCollectionViewCell" bundle:nil];
    
    [collectionView registerNib:nib forCellWithReuseIdentifier:reuserIdC];
    
    __weak HMHomeCollectionViewCell* cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:reuserIdC  forIndexPath:indexPath];
    
    CGFloat imgeW = [UIScreen mainScreen].bounds.size.width/8;
    
    CGFloat imageH = 69;
    
    CGFloat imageX = 7 * imgeW;
    
    CGFloat imageY = [UIScreen mainScreen].bounds.size.height - imageH/2;
    
    CGPoint endPonit = CGPointMake(imageX, imageY);
    
    cell1.model = self.searchResultArray[indexPath.item];
    
//    self.goodsCount = cell1.model.goodsCount;
    //监听 通知 确认 点击 了 几次
//     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(badgeNumberDidChange) name:@"changeGoodsNoti" object:cell1.model];
//    
    //动画
    cell1.reloadBlock = ^(CGRect rect){
        
        CGRect imageRect = [cell1 convertRect:cell1.hotImage.frame toView:self.view];
        
        NSURL* url = [NSURL URLWithString:cell1.model.img];
        
        CGPoint cotrollPoint = CGPointMake(endPonit.x, CGRectGetMidY(imageRect));
        
        UIBezierPath* path = [UIBezierPath bezierPath];
        
        [path moveToPoint:CGPointMake(CGRectGetMidX(imageRect), CGRectGetMidY(imageRect))];
        
        [path addQuadCurveToPoint:endPonit controlPoint:cotrollPoint];
        
        CALayer* layer = [CALayer layer];
        
        self.myLayer = layer;
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            layer.contents = (__bridge id _Nullable)(image.CGImage);
        }];
        
        layer.frame = CGRectMake(cell1.bounds.origin.x,cell1.bounds.origin.y, cell1.frame.size.width, cell1.frame.size.width);
        
        [[UIApplication sharedApplication].delegate.window.layer addSublayer:layer];
        
        CAAnimationGroup* group = [[CAAnimationGroup alloc]init];
        
        CABasicAnimation* scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        
        scale.fromValue = @0.8;
        
        scale.toValue = @0.00;
        
        CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        
        keyAnimation.path = path.CGPath;
        
        group.delegate = self;
        
        group.animations = @[scale,keyAnimation];
        
        group.duration = 2;
        
        group.removedOnCompletion = NO;
        
        group.fillMode = kCAFillModeForwards;
        
        group.timingFunction =[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
   
        [layer addAnimation:group forKey:nil];
    };

    return cell1;
 
}
//动画结束怎么办
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    if (anim == [self.myLayer animationForKey:@"group"]) {
        
        [self.myLayer removeFromSuperlayer];
        self.myLayer = nil;
    }
}



//隐藏 tabBar
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:254/255.0 green:211/255.0 blue:65/255.0 alpha:1]];
    
    [self.tabBarController.tabBar setHidden:NO];
    
    [self resignWindow];
    
//    [self.searchText removeObserver:self forKeyPath:@"text"];
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.tabBarController.tabBar setHidden:YES];
    
    //设置NAV
    [self setNAV];
    
    if (self.topView != nil) {
        
        [self addShoppingCart];
    }
    
    [self badgeNumberDidChange];
    
}

//设置 NAV
- (void)setNAV{
    
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:249/255.0 green:250/255.0 blue:253/255.0 alpha:1.0];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    
//    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(backToSuperMaket)];
    
    self.navigationController.navigationBar.translucent = false;
    
//    self.navigationItem.leftBarButtonItem = back;
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH -50, 30)];
    
    textField.delegate = self;
    
    self.searchText = textField;
    //设置键盘 enter类型
    textField.returnKeyType = UIReturnKeySearch;
    
    textField.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    [textField addTarget:self action:@selector(hiddenLabel:) forControlEvents:UIControlEventEditingChanged];
    
    UILabel *placeWord = [[UILabel alloc]init];
    
    self.placeWord = placeWord;
    placeWord.textAlignment = NSTextAlignmentCenter;
    placeWord.font = [UIFont systemFontOfSize:14];
    placeWord.textColor = [UIColor darkGrayColor];
    
    placeWord.text = @"请输入商品名称";
    
    UIImageView *searchIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_search"]];
    
    self.searchIcon = searchIcon;
    
    [textField addSubview:searchIcon];
    
    textField.layer.cornerRadius = 10;
    
    textField.clipsToBounds = YES;
    //KVO监听
//    [textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
    
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(textField).offset(-50);
        
        make.centerY.equalTo(textField);
    }];
    
    [textField addSubview:placeWord];
    
    [placeWord mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(searchIcon.mas_trailing);
        
        make.centerY.equalTo(textField);
    }];
    
    self.navigationItem.titleView = textField;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if([keyPath isEqualToString:@"text"]){
     
        self.searchText.leftView = self.searchIcon;
        
        self.searchText.leftViewMode = UITextFieldViewModeAlways;
        
        self.placeWord.hidden = YES;
    }else{
        
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
//删除观察者
- (void)dealloc{
    
//    [self.searchText removeObserver:self forKeyPath:@"text"];
    
//    [self.myLayer removeFromSuperlayer];
    
//    self.myLayer = nil;
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)backToSuperMaket{
    
    [self.navigationController popViewControllerAnimated:YES];
}


//搜索框代理

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.searchIcon.transform = CGAffineTransformTranslate(self.searchIcon.transform, -110, 0);
        
        self.placeWord.transform = CGAffineTransformTranslate(self.placeWord.transform, -95, 0);
        
        textField.leftView = self.searchIcon;
        
        textField.leftViewMode = UITextFieldViewModeWhileEditing;
        
    }];
}

- (void)hiddenLabel:(UITextField *)text{
    
    if (text.text.length > 0) {
        
        self.searchText.leftView = self.searchIcon;
        
        self.searchText.leftViewMode = UITextFieldViewModeAlways;
        
        self.placeWord.hidden = YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    UIButton *btn = [[UIButton alloc]init];
    
    [btn setTitle:textField.text forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(hotSearchDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self performSelector:@selector(hotSearchDidClick:) withObject:btn];
    
    return YES;
}



//购物车跳转

- (void)jumpToShoppingCart{
    
    ShoppingCartViewController *shoppingCartVC = [[ShoppingCartViewController alloc]init];
    
    UINavigationController *shoppingCartNAV = [[UINavigationController alloc]initWithRootViewController:shoppingCartVC];
    
    shoppingCartNAV.title = @"购物车";
    
     [self resignWindow];
    
    [self presentViewController:shoppingCartNAV animated:YES completion:nil];
    
   
    
}



- (void)addBackButton {
    
    UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [back setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
}


- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}






@end
